package com.zoffy.ybuhelper.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import java.io.*;

public class DataBaseUtil {
    Context context;
    private static final String dbName="YBU.db";
    private static String DATABASE_PATH;
    String databaseFilename;
    public DataBaseUtil(Context context){
        this.context=context;
        String packageName=context.getPackageName();
        DATABASE_PATH="/data/"+"data/"+packageName+"/databases/";
        databaseFilename=DATABASE_PATH+dbName;
    }
    public Boolean checkDataBase(){
        SQLiteDatabase db=null;
        try{
            db=SQLiteDatabase.openDatabase(databaseFilename,null,SQLiteDatabase.OPEN_READONLY);
        }catch (SQLiteException ignored){

        }
        return db!=null;
    }
    public void copyDataBase() throws IOException{
        File dir=new File(DATABASE_PATH);
        if(!dir.exists())
            dir.mkdir();
        FileOutputStream os=new FileOutputStream(databaseFilename);
        InputStream is=context.getAssets().open("YBU.db");
        byte[] buffer=new byte[8192];
        int count=0;
        while((count=is.read(buffer))>0){
            os.write(buffer,0,count);
            os.flush();
        }
        is.close();
        os.close();
    }

}
